package org.tanjor.common.shardingjdbc.shardrule.table;

import java.util.Collection;
import java.util.LinkedHashSet;

import org.tanjor.common.shardingjdbc.shardrule.ShardingUtil;

import com.dangdang.ddframe.rdb.sharding.api.ShardingValue;
import com.dangdang.ddframe.rdb.sharding.api.strategy.table.SingleKeyTableShardingAlgorithm;
import com.google.common.collect.Range;

public class TwoTableShardingAlgorithm implements SingleKeyTableShardingAlgorithm<String> {

	/** 
	    *  select * from t_order from t_order where order_id = 11  
	    *          └── SELECT *  FROM t_order_1 WHERE order_id = 11 
	    *  select * from t_order from t_order where order_id = 44 
	    *          └── SELECT *  FROM t_order_0 WHERE order_id = 44 
	    */  
		@Override
		public String doEqualSharding(Collection<String> tableNames, ShardingValue<String> shardingValue) {
			// TODO Auto-generated method stub
			String value = shardingValue.getValue()+"";
			Integer num = ShardingUtil.idConvertInt(value);
			for (String each : tableNames) {			
	            if (each.endsWith(num/2 % 2 + "")) {          
	                return each;  
	            }  
	        }  
	        throw new IllegalArgumentException();  
		}

	  /** 
	    *  select * from t_order from t_order where order_id in (11,44)   
	    *          ├── SELECT *  FROM t_order_0 WHERE order_id IN (11,44)  
	    *          └── SELECT *  FROM t_order_1 WHERE order_id IN (11,44)  
	    *  select * from t_order from t_order where order_id in (11,13,15)   
	    *          └── SELECT *  FROM t_order_1 WHERE order_id IN (11,13,15)   
	    *  select * from t_order from t_order where order_id in (22,24,26)   
	    *          └──SELECT *  FROM t_order_0 WHERE order_id IN (22,24,26)  
	    */  
		@Override
		public Collection<String> doInSharding(Collection<String> tableNames,
				ShardingValue<String> shardingValue) {
			// TODO Auto-generated method stub
			Collection<String> result = new LinkedHashSet<>(tableNames.size());  
			for(int i=0;i<shardingValue.getValues().size();i++){
				String value = shardingValue.getValues().toArray()[i]+"";
				Integer num = ShardingUtil.idConvertInt(value);
	            for (String tableName : tableNames) {              	
	                if (tableName.endsWith(num/2 % 2 + "")) {  
	                    result.add(tableName);  
	                }  
	            }  
	        }  
	        return result;  
		}

		@Override
		public Collection<String> doBetweenSharding(Collection<String> tableNames,
				ShardingValue<String> shardingValue) {
			// TODO Auto-generated method stub
			Collection<String> result = new LinkedHashSet<>(tableNames.size());  
	        Range<String> range = (Range<String>) shardingValue.getValueRange();  
	        for (Long i = Long.valueOf(range.lowerEndpoint()); i <= Long.valueOf(range.upperEndpoint()); i++) { 
	            for (String each : tableNames) {  
	                if (each.endsWith(i % 2 + "")) {  
	                    result.add(each);  
	                }  
	            }  
	        }  
	        return result;  
		}
}
